<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .box {
            width: 322px;
            position: fixed;
            bottom: 0;
            right: 0;
            overflow: hidden;
        }

        span {
            position: absolute;
            top: 0;
            right: 0;
            width: 30px;
            height: 20px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <span id="closeButton"></span>

    <div class="hd" id="headPart">
        <img src="img/kaijidonghua/t.jpg" alt=""/>
    </div>
    <div class="bd" id="bottomPart">
        <img src="img/kaijidonghua/b.jpg" alt=""/>
    </div>
</div>
<script>
    //找人
    var closeButton = document.getElementById("closeButton");
    var bottomPart = document.getElementById("bottomPart");
    var box = document.getElementById("box");
    //点击closeButton让bottomPart的高度渐渐地变为零然后让整个盒子的宽度变为零
    closeButton.onclick = function () {
        animate(bottomPart, {"height": 0}, function () {
            animate(box, {"width": 0});
        });
    }


    function animate(obj, json, fn) {
        clearInterval(obj.timer);
        obj.timer = setInterval(function () {
            var flag = true;
            //true表示所有属性的数值都到达目标了
            //先假设所有属性都到达目标了
            for (var k in json) {
                //json {属性名：属性值} {k:json[k]}
                var leader = parseInt(getStyle(obj, k)) || 0;
                var target = json[k];
                var step = (target - leader) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                leader = leader + step;
                console.log("target: " + target + "leader: " + leader + "step: " + step);
                obj.style[k] = leader + "px";
                if (leader != target) {
                    flag = false;//告诉人家 我还有没到的呢
                }
            }
            //最后再判断 如果标记仍然是true说明 没有没到达的了
            //也就是都到达了
            if (flag) {
                clearInterval(obj.timer);
                if (fn) {
                    fn();
                }
            }
        }, 15)
    }

    function getStyle(obj, attr) {
        if (obj.currentStyle) {
            return obj.currentStyle[attr];
        } else {
            return window.getComputedStyle(obj, null)[attr];
        }
    }

</script>
</body>
</html>
